package cn.edu.zut.internship.service.impl.teach;

import cn.edu.zut.internship.bean.sys.Result;
import cn.edu.zut.internship.iservice.notify.NotifySV;
import cn.edu.zut.internship.iservice.sys.IMenuService;
import cn.edu.zut.internship.mapper.sys.AuthSysUserMapper;
import cn.edu.zut.internship.mapper.sys.UserMapper;
import cn.edu.zut.internship.mapper.teach.TeachMapper;
import cn.edu.zut.internship.iservice.teach.TeachSV;
import cn.edu.zut.internship.service.impl.student.StudentSVImpl;
import cn.edu.zut.internship.utils.ExcelUtils;
import cn.edu.zut.internship.utils.PrimaryKeyUtil;
import cn.edu.zut.internship.utils.ThreadPoolUtil;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class TeachSVImpl implements TeachSV {
    @Autowired
    TeachMapper teachMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    private IMenuService menuService;
    @Autowired
    PrimaryKeyUtil primaryKeyUtil;
    @Autowired
    private AuthSysUserMapper authUserMapper;
    @Value("${zn.edu.zut.internship.localPath}")
    private String localPath;
    @Autowired
    NotifySV notifySV;
    @Override
    public Result getTeachById(Map params) {
        Result result = new Result();
        Map teachById = teachMapper.getTeachById(params);
        result.setBean(teachById);
        result.setCode(20000);
        result.setMessage("获取成功");
        return result;
    }

    @Override
    public Result selectTeach(Map params) {
        return null;
    }

    @Override
    public Result delectTeach(String teachId) {
        return null;
    }

    @Override
    public Result updataTeach(Map params) {
        return null;
    }

    @Override
    public Result importTeach(Map params) {
        Result result = new Result();
        ThreadPoolUtil.getThreadPool().execute(new task(params));
        result.setMessage("导入任务执行中！");
        return result;
    }

    @Override
    public Result getMyTeach(String studentAccount) {
        Result result = new Result();
        Map myTeach = teachMapper.getMyTeach(studentAccount);
        result.setBean(myTeach);
        return result;
    }
    class task extends  Thread {
        private Map params;

        public task(Map params) {
            this.params = params;
        }

        @Override
        public void run() {
            String creatAccount = (String) params.get("creatAccount");
            //创建提醒消息
            Map notify = new HashMap();
            notify.put("sendAccount","admin");
            notify.put("sendType",0);
            notify.put("notifyType",0);
            notify.put("targetAccount",creatAccount);
            notify.put("targetType",0);
            notify.put("creatAccount",creatAccount);
            try {
                String filePath = (String) params.get("filePath");
                //处理文件路径为本地路径
                String fileName = filePath.substring(filePath.lastIndexOf("/")+1);
                String LocalFilePath = localPath+fileName;
                List<List<Map<String, Object>>> lists = ExcelUtils.importExcel(LocalFilePath, 1, 1, -1);
                String[] keys = {"jobNumber","teachName","teachPhone","teachGender"};
                List<Map<String, Object>> list = ExcelUtils.getList(keys, lists.get(0));
                if (!CollectionUtils.isEmpty(list)) {
                    for (Map map : list) {
                        //将此条教师信息处理后添加进系统
                        //性别转换
                        String studentGender = (String) map.get("studentGender");
                        if(StringUtils.isEmpty(studentGender)){
                            map.put("studentGender",-1);
                        }else if(studentGender.equals("男")){
                            map.put("studentGender",0);
                        }else if (studentGender.equals("女")){
                            map.put("studentGender",1);
                        }else{
                            map.put("studentGender",-1);
                        }
                        //添加进系统用户表
                        Long userId = primaryKeyUtil.generatePrimaryKey("sys_user");
                        Map user = new HashMap();
                        user.put("userId",userId);
                        user.put("userAccount",map.get("jobNumber"));
                        user.put("userPhone",map.get("teachPhone"));
                        user.put("userName",map.get("teachName"));
                        user.put("userPwd",map.get("teachPhone"));
                        user.put("sex",map.get("teachGender"));
                        user.put("userType",1);
                        user.put("creatAccount",creatAccount);
                        userMapper.addUser(user);
                        //添加进教师表
                        Long teachId = primaryKeyUtil.generatePrimaryKey("teach");
                        map.put("teachId",teachId);
                        map.put("creatAccount",creatAccount);
                        map.put("userId",userId);
                        teachMapper.addTeach(map);
                        //绑定角色信息
                        authUserMapper.assignRole(userId,2L);
                    }
                }
                notify.put("content","导入教师任务执行成功！");
            }catch (Exception exception){
                notify.put("content","导入教师任务执行失败！"+exception.getMessage());
            }
            //发送提醒消息
            notifySV.addNotify(notify);
        }
    }

}
